package editor.widgets;

import editor.Images;
import widgets.CustomDialog;

import javax.swing.*;
import java.awt.*;

/**
 * This class is a progress bar displaying an image as the background and an image as the bar.
 * 
 * @author Neil Dickson
 */
public class CustomProgressBar extends JComponent {
	private	String	text;
	private	float	completion;

	public static final int		HEIGHT		= 18;
	public static final Image	BG_MID_256	= Images.PROGRESS_MID.getScaledInstance(256,HEIGHT,Image.SCALE_REPLICATE);
	public static final Image	FG_MID_256	= Images.PROGRESS_FILL_MID.getScaledInstance(256,HEIGHT-2,Image.SCALE_REPLICATE);

	static {
		// Ensure that the scaled images get loaded immediately
		while (BG_MID_256.getWidth(null)<256 || FG_MID_256.getWidth(null)<256 || BG_MID_256.getHeight(null)<HEIGHT || FG_MID_256.getHeight(null)<HEIGHT-2) {
			try {
				Thread.sleep(0);
			}
			catch (InterruptedException e) {
				// This exception doesn't matter
			}
		}
	}

	/**
	 * Creates a new CustomProgressBar
	 * @param width the initial width of the progress bar
	 * @param completion the initial conpletion (from 0.0f to 1.0f)
	 */
	public CustomProgressBar(int width,float completion) {
		this(width,null,completion);
	}
	public CustomProgressBar(int width,String text,float completion) {
		setSize(width,HEIGHT);
		this.completion = completion;
		this.text = text;
	}

	@Deprecated
	public void reshape(int x, int y, int w, int h) {
		super.reshape(x, y, Math.max(w,0), HEIGHT);
	}

	public Dimension getPreferredSize() {
		return new Dimension(getWidth(),HEIGHT);
	}

	public float getCompletion() {
		return completion;
	}
	
	public void setCompletion(float completion) {
		this.completion = completion;
		repaint();
	}

	public void setStatus(String text,float completion) {
		this.text = text;
		this.completion = completion;
		repaint();
	}

	public void paintComponent(Graphics g) {
		int width = getWidth();
		int fillWidth = Math.max((int)(completion*(width-2)),0);

		// Draw Background
		int xMidStart = Images.PROGRESS_FILL_LEFT.getWidth(null);
		int xMidEnd = width-Images.PROGRESS_RIGHT.getWidth(null);
		g.drawImage(Images.PROGRESS_LEFT,0,0,null);
		int i;
		for (i=xMidStart;i+255 < xMidEnd;i+=256) {
			g.drawImage(BG_MID_256,i,0,null);
		}
		g.drawImage(BG_MID_256,i,0,xMidEnd&0xFF,HEIGHT,null);
		g.drawImage(Images.PROGRESS_RIGHT,xMidEnd,0,null);

		// Draw Fill
		xMidStart = Images.PROGRESS_FILL_LEFT.getWidth(null)+1;
		xMidEnd = Math.max(fillWidth-1-Images.PROGRESS_FILL_RIGHT.getWidth(null),0);
		g.drawImage(Images.PROGRESS_FILL_LEFT,1,1,Math.min(xMidStart-1,fillWidth>>1),HEIGHT-2,null);
		for (i=xMidStart;i+255 < xMidEnd;i+=256) {
			g.drawImage(FG_MID_256,i,1,null);
		}
		g.drawImage(FG_MID_256,i,1,xMidEnd&0xFF,HEIGHT-2,null);
		g.drawImage(Images.PROGRESS_FILL_RIGHT,xMidEnd,1,Math.max(Math.min(Images.PROGRESS_FILL_RIGHT.getWidth(null),fillWidth>>1),0),HEIGHT-2,null);

		if (text!=null && text.length()>0) {
			g.setColor(Color.WHITE);
			g.setFont(CustomDialog.FONT);
			g.drawString(text,4,Math.max((HEIGHT+CustomDialog.FONT.getSize())>>1,2)-2);
		}
	}
}